package com.special.ResideMenuDemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;
import com.special.base.BaseFrameActivity;

public class MenuActivity extends BaseFrameActivity implements
		View.OnClickListener {

	private ResideMenu resideMenu;
	private MenuActivity mContext;
	private ResideMenuItem itemHome;
	private ResideMenuItem itemProfile;
	private ResideMenuItem itemMap;
	private ResideMenuItem itemSettings;
	/*
	 * Is menu opening?
	 */
	private boolean isOpening = false;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = this;
		setUpMenu();
		changeFragment(new MapFragment());
	}

	private void setUpMenu() {

		// attach to current activity;
		resideMenu = new ResideMenu(this);
		resideMenu.setBackground(R.drawable.menu_background);
		resideMenu.attachToActivity(this);
		resideMenu.setMenuListener(menuListener);

		// create menu items;
		itemHome = new ResideMenuItem(this, R.drawable.icon_home, "Home");
		itemProfile = new ResideMenuItem(this, R.drawable.icon_profile,
				"Profile");
		itemMap = new ResideMenuItem(this,
				android.R.drawable.ic_menu_mapmode, "Map");
		itemSettings = new ResideMenuItem(this, R.drawable.icon_settings,
				"Settings");

		itemHome.setOnClickListener(this);
		itemProfile.setOnClickListener(this);
		itemMap.setOnClickListener(this);
		itemSettings.setOnClickListener(this);

		resideMenu.addMenuItem(itemHome);
		resideMenu.addMenuItem(itemProfile);
		resideMenu.addMenuItem(itemMap);
		resideMenu.addMenuItem(itemSettings);

		findViewById(R.id.title_bar_menu).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						resideMenu.openMenu();
					}
				});
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return resideMenu.onInterceptTouchEvent(ev)
				|| super.dispatchTouchEvent(ev);
	}

	@Override
	public void onClick(View view) {

		if (view == itemHome) {
			changeFragment(new HomeFragment());
		} else if (view == itemProfile) {
			changeFragment(new ProfileFragment());
		} else if (view == itemMap) {
			changeFragment(new MapFragment());
		} else if (view == itemSettings) {
			changeFragment(new SettingsFragment());
		}

		resideMenu.closeMenu();
	}

	private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
		@Override
		public void openMenu() {
//			Toast.makeText(mContext, "Menu is opened!", Toast.LENGTH_SHORT).show();
			isOpening = true;
		}

		@Override
		public void closeMenu() {
//			Toast.makeText(mContext, "Menu is closed!", Toast.LENGTH_SHORT).show();
			isOpening = false;
		}
	};

	private void changeFragment(Fragment targetFragment) {
		resideMenu.clearIgnoredViewList();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_fragment, targetFragment, "fragment")
				.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
				.commit();
	}

	// What good method is to access resideMenu？
	public ResideMenu getResideMenu() {
		return resideMenu;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (!isOpening) {
				// 如果菜单关闭了，按返回键打开菜单
				resideMenu.openMenu();
				return false; //无需再处理
			}
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub

	}
}
